package parkingManage.utils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Scanner;

import Util.TestUtil;
import parkingManage.dao.ParkingDao;
import parkingManage.dao.RecordDao;
import parkingManage.dao.impl.ParkingDaoImpl;
import parkingManage.dao.impl.RecordDaoImpl;
import parkingManage.model.RecordModel;

public class RecordUtils {

	static Scanner sc = new Scanner(System.in);

	/**
	 * 费用
	 */
	public static BigDecimal total = new BigDecimal("0");

	/**
	 * 获取总费用
	 */
	static {
		RecordDao recordDao = new RecordDaoImpl();
		List<RecordModel> records = recordDao.selectAll();
		for (RecordModel re : records) {
			total = total.add(re.getFree());
		}
	}

	/**
	 * 查询全部记录
	 * 
	 * 已缴费
	 */
	public void selectAll() {
		RecordDao recordDao = new RecordDaoImpl();

		List<RecordModel> records = recordDao.selectAll();

		System.out.println("============全部缴费记录============");
		for (RecordModel re : records) {
			info(re);
		}
		System.out.println("总收费：" + RecordUtils.total + "元");
		System.out.println();
	}

	/**
	 * 全部未缴费记录
	 */
	public void unpaid() {
		RecordDao recordDao = new RecordDaoImpl();

		List<RecordModel> records = recordDao.unpaid();

		System.out.println("===========全部未缴费记录===========");
		for (RecordModel re : records) {
			info(re);
		}

	}

	/**
	 * 根据车牌号
	 * 
	 * 查询停车记录
	 */
	public void selectByCar() {
		RecordDao record = new RecordDaoImpl();
		System.out.print("请输入您要查询的车牌号：");
		String license = sc.nextLine();
		List<RecordModel> recordPay = record.selectByCarFree(license);
		if (recordPay.size() != 0) {
			for (RecordModel re : recordPay) {
				info(re);
			}
		}
		List<RecordModel> recordUnpay = record.selectByCarUnfree(license);
		if (recordUnpay.size() != 0) {
			for (RecordModel re : recordUnpay) {
				info(re);
			}
		}

		if (recordPay.size() == 0 && recordUnpay.size() == 0)
			System.out.println("未查到相关车辆！");
	}

	/**
	 * 记录信息
	 * 
	 * @param re
	 */
	public void info(RecordModel re) {
		if (re == null) {
			System.out.println("未查到相关记录...");
			return;
		}
		System.out.println("-----------------------------------");
		System.out.println("车牌号：" + re.getCarLicense());
		System.out.println("车位号：" + re.getLotCode());
		System.out.println("进入时间：" + re.getStartTime());
		if (re.getLeaveTime() != null) {
			System.out.println("离开时间：" + re.getLeaveTime());
			System.out.println("费用：" + re.getFree() + "元");
			System.out.println("-----------------------------------");
			return;
		}
		System.out.println("-----------------------------------");
	}

	/**
	 * 添加一条停车记录
	 */
	public void addRecord(String license) {
		System.out.print("请选择一个车位：");
		while (true) {
			int lotCode = TestUtil.abnormal();
			ParkingUtils parkingUtils = new ParkingUtils();
			if (parkingUtils.judge(lotCode)) {// true 未被占用
				// 将车位状态置1
				ParkingDao parkingDao = new ParkingDaoImpl();
				parkingDao.park(lotCode);

				RecordModel recordModel = new RecordModel();
				recordModel.setCarLicense(license);
				recordModel.setLotCode(lotCode);
				recordModel.setStartTime();

				RecordDao recordDao = new RecordDaoImpl();
				recordDao.addRecord(license, lotCode, recordModel.getStartTime());
				return;
			}

			System.out.print("该车位已被占用，请重新选择：");
		}
	}

	/**
	 * 取车
	 */
	public void pickup() {
		System.out.print("请输入您的车牌号：");
		String license = sc.nextLine();
		System.out.print("请输入您的车位号：");
		int lotCode = TestUtil.abnormal();

		// 查车牌号，车位号
		RecordDao recordDao = new RecordDaoImpl();
		RecordModel record = recordDao.pickupInfo(license, lotCode);

		ParkingDao park = new ParkingDaoImpl();

		if (record != null) {
			record.setLeaveTime();
			record.setFree();

			int k = recordDao.pickup(record.getId(), record.getLeaveTime(), record.getFree());
			int n = park.pickup(lotCode);

			if (k == 1 && n == 1) {
				System.out.println("请缴纳费用：" + record.getFree() + "元");
				System.out.println("点击回车缴费...");
				sc.nextLine();
				System.out.println("缴费成功！");
				return;
			}
		}
		System.out.println("失败");
	}

	public void start() {
		while (true) {
			System.out.println("===================================");
			System.out.println("请选择：");
			System.out.println("1-查询全部缴费记录");
			System.out.println("2-查询全部未缴费记录");
			System.out.println("3-根据车牌号查询记录");
			System.out.println("4-查看总收费");
			System.out.println("5-退出");
			System.out.println("===================================");

			int key = TestUtil.abnormal();
			if (key == 5)
				break;

			switch (key) {
			case 1:
				selectAll();
				break;
			case 2:
				unpaid();
				break;
			case 3:
				selectByCar();
				break;
			case 4:
				System.out.println("总收费：" + total + "元");
				break;
			default:
				break;
			}
			System.out.println("点击回车继续...");
			sc.nextLine();
		}
	}

}
